package com.fingard.dsp.bank.directbank.cib03;

import com.fingard.common.utils.json.JSONUtils;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;

import java.util.HashMap;

public class CIB03SinglePay extends CIB03Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
    	RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail = d_190802(p_req1908);
        tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }
    
    private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
        
        try {
        	ActSetItem actItem = getBankConfig().getActSetItem(p_req1908.singleDetail.payAct, "");
        	if(actItem == null){
        		tmpRetDetail.respCode = "0";
        		tmpRetDetail.respInfo = "未找到账号[" + p_req1908.singleDetail.payAct + "]批量商户配置信息";
        		tmpRetDetail.transAmt = p_req1908.singleDetail.payAmount;
            	tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            	tmpRetDetail.transState = PaymentState.FAIL_Payment;
            	tmpRetDetail.payInfoCode = FailCode.E9981;

            	return tmpRetDetail;
        	} 
        	
            HashMap<String, String> postMapParams = new HashMap<String, String>();
            postMapParams.put("version", "1.0.2");
            postMapParams.put("mchtId", actItem.merchantId);
            postMapParams.put("signType", "RSA");
            postMapParams.put("serialNo", p_req1908.singleDetail.askNumber);
            postMapParams.put("transTime", Format.DateTime14Format.format(p_req1908.singleDetail.getPayDateTime()));
            postMapParams.put("subMchtName", "");
            postMapParams.put("acctName", encryptByCFCA(actItem,p_req1908.singleDetail.recName));
            postMapParams.put("acctType", "");
            postMapParams.put("acctNo", encryptByCFCA(actItem, p_req1908.singleDetail.recAct));
            postMapParams.put("amount", p_req1908.singleDetail.payAmount);
            postMapParams.put("currency", "156");
            
            String[] findValues = new String[2];
            findValues[0] = p_req1908.singleDetail.recBankCode;
			findValues[1] = p_req1908.singleDetail.recAreaName;
			HashMap<String,String> BANK_CODE = this.getBankConfig().getMapAdvanceValues("BankCodeMapping", new String[]{"ATSBankCode","ATSAreaName"}, new String[]{"value"}, findValues);
			if(BANK_CODE != null && BANK_CODE.size() > 0 && BANK_CODE.containsKey("value")){
				postMapParams.put("bankNo", BANK_CODE.get("value")); 
			} else {
				postMapParams.put("bankNo", "");
			}
            
            String desc = p_req1908.singleDetail.description;
            if (StringHelper.isNullOrEmpty(desc)) {
            	desc = p_req1908.singleDetail.memo;
            }
            postMapParams.put("desc", desc);
            postMapParams.put("bankName", p_req1908.singleDetail.cnapsName);
            String isPrivate = p_req1908.singleDetail.isPrivate;
            if ("1".equals(isPrivate)) {
            	postMapParams.put("pubOrPriFlag", "1");
            } else {
            	postMapParams.put("pubOrPriFlag", "0");
            }
            postMapParams.put("bankCode", p_req1908.singleDetail.cnapsCode);
            postMapParams.put("mac", generateMAC(postMapParams, actItem));
            
            String tmpStrRet = sendDataToBank(JSONUtils.map2JSON(postMapParams), "singlePaySubmit");
            if (StringHelper.hasAnyChar(tmpStrRet)) {
        		if (tmpStrRet.indexOf("{") >= 0) {
        			HashMap<String, String> tmpJsonRet = JSONUtils.json2Map(tmpStrRet);
        			
        			tmpRetDetail.respCode = "0";
    				tmpRetDetail.respInfo = tmpJsonRet.get("respMsg");
    				tmpRetDetail.reqSeqID = tmpJsonRet.get("serialNo");
    				tmpRetDetail.transState = tmpJsonRet.get("respCode");
    				tmpRetDetail.payInfoCode = tmpJsonRet.get("respCode");
        		} else {
        			tmpRetDetail.respCode = "-1";
                	tmpRetDetail.respInfo = "银行响应数据不合法";
        		}
        	} else {
        		tmpRetDetail.respCode = "-1";
            	tmpRetDetail.respInfo = "银行响应为空";
        	}
        } catch (Exception ex) {
        	ex.printStackTrace();
        	tmpRetDetail.respCode = "-1";
        	tmpRetDetail.respInfo = ex.getMessage();
        	WriteBankLogLn(ex);
        }
        
        return tmpRetDetail;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();

        try {
        	ActSetItem actItem = getBankConfig().getActSetItem(p_req1944.payAct, "");
        	if (actItem == null) {
        		tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = "未找到账号[" + p_req1944.payAct + "]商户配置信息";
				retAts.writeRetDetail(tmpRetDetail);
				return retAts;
			}
            
            HashMap<String, String> postMapParams = new HashMap<String, String>();
            postMapParams.put("version", "1.0.2");
            postMapParams.put("mchtId", actItem.merchantId);
            postMapParams.put("signType", "RSA");
            postMapParams.put("serialNo", p_req1944.askNumber);
            postMapParams.put("transTime", Format.DateTime14Format.format(p_req1944.getBgnDateTime()));
            postMapParams.put("mac", generateMAC(postMapParams, actItem));
            
            String tmpStrRet = sendDataToBank(JSONUtils.map2JSON(postMapParams), "singlePayQuery");
        	if (StringHelper.hasAnyChar(tmpStrRet)) {
        		if (tmpStrRet.indexOf("{") >= 0) {
        			HashMap<String, String> tmpJsonRet = JSONUtils.json2Map(tmpStrRet);
        			
        			boolean verifyResult = verifyMAC(tmpJsonRet, actItem);
        			System.out.println(verifyResult);
        			
    				String tmpRespCode = tmpJsonRet.get("respCode");
    				String tmpRespInfo = tmpJsonRet.get("respMsg");
    				String tranStatus = tmpJsonRet.get("tranStatus");
    				
    				tmpRetDetail.respCode = "0";
    				tmpRetDetail.respInfo = tmpRespInfo;
    				tmpRetDetail.reqSeqID = tmpJsonRet.get("serialNo");
    				
    				if (StringHelper.isNullOrEmpty(tranStatus) || "null".equals(tranStatus)) {
    					if ("E0523".equals(tmpRespCode)) {
    						if(canBeNotExist(p_req1944, tmpRetDetail, 20)){
    							tmpRetDetail.transAmt = tmpJsonRet.get("amount");
                				tmpRetDetail.transState = tmpRespCode;
                				tmpRetDetail.payInfoCode = tmpRespCode;
    						}
    					} else {
    						tmpRetDetail.respCode = "-1";
            				tmpRetDetail.respInfo = tmpRespInfo;
    					}
    				} else {
    					tmpRetDetail.transAmt = tmpJsonRet.get("amount");
        				tmpRetDetail.transState = StringHelper.joinNotEmpty("+", tranStatus, tmpRespCode);
        				tmpRetDetail.payInfoCode = tmpRespCode;
    				}
        		} else {
        			tmpRetDetail.respCode = "-1";
                	tmpRetDetail.respInfo = "银行响应数据不合法";
        		}
        	} else {
        		tmpRetDetail.respCode = "-1";
            	tmpRetDetail.respInfo = "银行响应为空";
        	}
        } catch (Exception ex) {
        	ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        
        if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req1944.askNumber;
        }
        
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
